<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>修改申请 - 售后管理系统</title>
    <style>
        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
        }

        body {
            font-family: 'Microsoft YaHei', Arial, sans-serif;
            background-color: #f5f5f5;
        }

        .header {
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            color: white;
            padding: 20px;
            display: flex;
            justify-content: space-between;
            align-items: center;
        }

        .header h1 {
            font-size: 24px;
        }

        .back-btn {
            background: rgba(255, 255, 255, 0.2);
            border: 1px solid rgba(255, 255, 255, 0.3);
            color: white;
            padding: 8px 16px;
            border-radius: 5px;
            cursor: pointer;
            text-decoration: none;
        }

        .container {
            max-width: 800px;
            margin: 0 auto;
            padding: 20px;
        }

        .form-card {
            background: white;
            border-radius: 10px;
            padding: 30px;
            box-shadow: 0 2px 10px rgba(0, 0, 0, 0.1);
        }

        .form-title {
            font-size: 18px;
            color: #333;
            margin-bottom: 20px;
            padding-bottom: 10px;
            border-bottom: 1px solid #e1e5e9;
        }

        .form-group {
            margin-bottom: 20px;
        }

        .form-label {
            display: block;
            margin-bottom: 8px;
            font-weight: 500;
            color: #333;
        }

        .form-control {
            width: 100%;
            padding: 10px 12px;
            border: 2px solid #e1e5e9;
            border-radius: 5px;
            font-size: 14px;
            transition: border-color 0.2s ease;
        }

        .form-control:focus {
            border-color: #667eea;
            outline: none;
        }

        textarea.form-control {
            min-height: 120px;
            resize: vertical;
        }

        .form-actions {
            display: flex;
            justify-content: flex-end;
            gap: 15px;
            margin-top: 30px;
        }

        .btn {
            padding: 10px 20px;
            border-radius: 5px;
            font-size: 14px;
            cursor: pointer;
            transition: all 0.2s ease;
        }

        .btn-default {
            background: white;
            color: #333;
            border: 1px solid #e1e5e9;
        }

        .btn-default:hover {
            background: #f8f9fa;
        }

        .btn-primary {
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            color: white;
            border: none;
        }

        .btn-primary:hover {
            opacity: 0.9;
        }

        .loading {
            text-align: center;
            padding: 40px;
        }

        .spinner {
            border: 2px solid #f3f3f3;
            border-top: 2px solid #667eea;
            border-radius: 50%;
            width: 30px;
            height: 30px;
            animation: spin 1s linear infinite;
            display: inline-block;
        }

        @keyframes spin {
            0% { transform: rotate(0deg); }
            100% { transform: rotate(360deg); }
        }

        .error-message {
            color: #dc3545;
            font-size: 12px;
            margin-top: 5px;
        }
    </style>
</head>
<body>
<div class="header">
    <h1>修改售后申请</h1>
    <a href="/after_sale_system/page/consumer/applicationList" class="back-btn">返回列表</a>
</div>

<div class="container">
    <div class="form-card">
        <div class="form-title">申请信息</div>

        <form id="editForm">
            <input type="hidden" id="applicationId">

            <div class="form-group">
                <label class="form-label">申请类型</label>
                <select id="afterSaleType" class="form-control" disabled>
                    <option value="RETURN">退货</option>
                    <option value="EXCHANGE">换货</option>
                    <option value="REPAIR">维修</option>
                </select>
            </div>

            <div class="form-group">
                <label class="form-label">订单号</label>
                <input type="text" id="orderId" class="form-control" disabled>
            </div>

            <div class="form-group">
                <label class="form-label">商品名称</label>
                <input type="text" id="productName" class="form-control" disabled>
            </div>

            <div class="form-group">
                <label class="form-label">问题描述 <span style="color: red;">*</span></label>
                <textarea id="problemDescription" class="form-control" required></textarea>
                <div class="error-message" id="problemDescriptionError"></div>
            </div>

            <div class="form-group">
                <label class="form-label">联系方式</label>
                <input type="text" id="contactInfo" class="form-control" required>
                <div class="error-message" id="contactInfoError"></div>
            </div>

            <div class="form-group">
                <label class="form-label">收货地址（仅退货/换货）</label>
                <textarea id="shippingAddress" class="form-control"></textarea>
            </div>

            <div class="form-actions">
                <button type="button" class="btn btn-default" onclick="window.location.href='/after_sale_system/page/consumer/applicationList'">取消</button>
                <button type="submit" class="btn btn-primary">保存修改</button>
            </div>
        </form>
    </div>
</div>

<script>
    // 获取URL中的申请ID
    function getApplicationId() {
        const params = new URLSearchParams(window.location.search);
        return params.get('id');
    }

    // 加载申请详情
    function loadApplicationDetails() {
        const id = getApplicationId();
        if (!id) {
            alert('无效的申请ID');
            return;
        }

        fetch(`/after_sale_system/api/afterSale/detail/${id}`)
            .then(response => response.json())
            .then(data => {
                if (data.success) {
                    populateForm(data.data);
                } else {
                    alert('获取申请详情失败: ' + data.message);
                }
            })
            .catch(error => {
                console.error('获取申请详情失败:', error);
                alert('获取申请详情失败，请稍后重试');
            });
    }

    // 填充表单数据
    function populateForm(application) {
        document.getElementById('applicationId').value = application.id;
        document.getElementById('afterSaleType').value = application.afterSaleType || '';
        document.getElementById('orderId').value = application.orderId || '';
        document.getElementById('productName').value = application.productName || '';
        document.getElementById('problemDescription').value = application.applyReason || '';
        document.getElementById('contactInfo').value = application.contactInfo || '';
        document.getElementById('shippingAddress').value = application.shippingAddress || '';

        // 如果是维修类型，隐藏收货地址
        if (application.afterSaleType === 'REPAIR') {
            document.querySelector('label[for="shippingAddress"]').parentElement.style.display = 'none';
        }
    }

    // 验证表单
    function validateForm() {
        let isValid = true;
        const problemDescription = document.getElementById('problemDescription').value.trim();
        const contactInfo = document.getElementById('contactInfo').value.trim();

        // 验证问题描述
        if (!problemDescription) {
            document.getElementById('problemDescriptionError').textContent = '请输入问题描述';
            isValid = false;
        } else {
            document.getElementById('problemDescriptionError').textContent = '';
        }

        // 验证联系方式
        if (!contactInfo) {
            document.getElementById('contactInfoError').textContent = '请输入联系方式';
            isValid = false;
        } else {
            document.getElementById('contactInfoError').textContent = '';
        }

        return isValid;
    }

    // 保存修改
    function saveApplication() {
        if (!validateForm()) {
            return;
        }

        const id = document.getElementById('applicationId').value;
        const formData = {
            problemDescription: document.getElementById('problemDescription').value.trim(),
            contactInfo: document.getElementById('contactInfo').value.trim(),
            shippingAddress: document.getElementById('shippingAddress').value.trim()
        };

        fetch(`/after_sale_system/api/afterSale/${id}`, {
            method: 'PUT',
            headers: {
                'Content-Type': 'application/json'
            },
            body: JSON.stringify(formData)
        })
            .then(response => response.json())
            .then(data => {
                if (data.success) {
                    alert('申请修改成功');
                    window.location.href = '/after_sale_system/page/consumer/applicationList';
                } else {
                    alert('修改失败: ' + data.message);
                }
            })
            .catch(error => {
                console.error('修改申请失败:', error);
                alert('修改申请失败，请稍后重试');
            });
    }

    // 页面加载时初始化
    window.addEventListener('load', function() {
        loadApplicationDetails();

        // 绑定表单提交事件
        document.getElementById('editForm').addEventListener('submit', function(e) {
            e.preventDefault();
            saveApplication();
        });
    });
</script>
</body>
</html>